#include <TopoDS_Shape.hxx>
#include <BRepTools.hxx>
#include <STEPControl_Reader.hxx>
#include <vtkRenderWindow.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkActor.h>
#include <vtkPolyDataMapper.h>
#include <vtkNew.h>
#include <vtkRenderer.h>
#include <iostream>
#include <IVTKTools_ShapeDataSource.hxx>

int main() {
    // 创建一个 STEP 读取器
    STEPControl_Reader reader;
    
    // 读取 STEP 文件
    IFSelect_ReturnStatus status = reader.ReadFile("../airplane.stp");
    if (status != IFSelect_RetDone) {
        std::cout << "Error: Cannot read the STEP file." << std::endl;
        return 1;
    }

    // 转换
    reader.TransferRoots();
    TopoDS_Shape shape = reader.OneShape();

    vtkNew<vtkRenderWindow> renderWindow;
    vtkNew<vtkRenderer> renderer;
    renderWindow->AddRenderer(renderer);
    
    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
    renderWindowInteractor->SetRenderWindow(renderWindow);
    vtkNew<vtkInteractorStyleTrackballCamera> style;
    renderWindowInteractor->SetInteractorStyle(style);

    IVtkOCC_Shape::Handle aShapeImpl = new IVtkOCC_Shape(shape);
    vtkSmartPointer<IVtkTools_ShapeDataSource> DS = vtkSmartPointer<IVtkTools_ShapeDataSource>::New();
    DS->SetShape(aShapeImpl);

    vtkSmartPointer<vtkPolyDataMapper> Mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    Mapper->SetInputConnection(DS->GetOutputPort());
    vtkSmartPointer<vtkActor> Actor = vtkSmartPointer<vtkActor>::New();
    Actor->SetMapper(Mapper);

    renderer->AddActor(Actor);
    renderer->ResetCamera(); // 重置相机位置以确保能看到完整的模型

    renderWindow->Render();
    renderWindowInteractor->Start();

    std::cout << "Successfully loaded and displayed the STEP file!" << std::endl;
    return 0;
}